package com.overseasShopping.Base.DAOImpl;


import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


import com.overseasShopping.Base.DAO.GoodsDAO;
import com.overseasShopping.pojo.Goods;
import com.overseasShopping.util.PageBean;

public class GoodsDAOImpl extends BaseDAOImpl<Goods> implements GoodsDAO {
   
	public List<Goods> getAll() {
		String hql = "from Goods ";
		List<Goods> goods = getHibernateTemplate().find(hql);
		return goods;
	}

	public List<Goods> getGoodsByListType(String listType) {
		String hql = "from Goods where listType = ? where state=1";
		List<Goods> goods = getHibernateTemplate().find(hql, listType);
		return goods;
	}

	public List<Goods> getGoodsByType(int type) {
		String hql = "from Goods where type = ?  where state=1";
		List<Goods> goods = getHibernateTemplate().find(hql, type);
		return goods;
	}

	public void addGoods(Goods good) {
		getHibernateTemplate().save(good);
	}

	public List<Goods> getGoodsByTemp(String temp) {
		String hql = "from Goods where goodName like '%" + temp + "%' and state=1";
		List<Goods> goods = getHibernateTemplate().find(hql);
		return goods;
	}

	public List<Goods> getGoodByName(String goodName) {
		String hql = "from Goods where goodName = ?  and state=1";
		List<Goods> goods = getHibernateTemplate().find(hql, goodName);
		return goods;
	}

	public Goods getGood(String goodId) {
		String hql = "from Goods where goodId=? and state=1";
		return (Goods) getHibernateTemplate().find(hql, goodId).get(0);
	}

	public List<Goods> getRecentlyGoods() {
		String hql = "from Goods where state=1 order by createDate desc";
		List<Goods> goods = getHibernateTemplate().find(hql).subList(0, 6);
		return goods;
	}

	public void getGoodsByListTypePage(String listType, PageBean<Goods> pageBean) {
		int totalCount = this.getTotalCount(listType);
		pageBean.setTotalCount(totalCount);
		
		if (pageBean.getCurrentPage() <=0) {
			pageBean.setCurrentPage(1);					    
		} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
			pageBean.setCurrentPage(pageBean.getTotalPage());		
		}
		
		int currentPage = pageBean.getCurrentPage();
		int index = (currentPage-1)*pageBean.getPageSize();
		int count = pageBean.getPageSize();
		
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		org.hibernate.classic.Session session = factory.openSession();
		
		String hql = "from Goods where listType = ? and state=1";
		Query query = session.createQuery(hql);
		query.setParameter(0, listType);
		query.setFirstResult(index);
		query.setMaxResults(count);
		
		pageBean.setPageData(query.list());
	}

	public int getTotalCount(String listType) {
		String hql = "select count(*) from Goods where listType = ? and state=1";
		int totalCount = Integer.parseInt(String.valueOf(getHibernateTemplate()
				.find(hql, listType).iterator().next()));		
		return totalCount;
	}

	public void getGoodsByListTypePage(String listType, int type,
			PageBean<Goods> pageBean) {
		int totalCount = getTotalCount(listType,type);
		
		pageBean.setTotalCount(totalCount);
		
		if (pageBean.getCurrentPage() <=0) {
			pageBean.setCurrentPage(1);					    
		} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
			pageBean.setCurrentPage(pageBean.getTotalPage());		
		}
		
		int currentPage = pageBean.getCurrentPage();
		int index = (currentPage-1)*pageBean.getPageSize();
		int count = pageBean.getPageSize();
		
		
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		org.hibernate.classic.Session session = factory.openSession();
		
		String hql = "from Goods where listType = ? and type =? and state=1";
		Query query = session.createQuery(hql);
		query.setParameter(0, listType);
		query.setParameter(1, type);
		query.setFirstResult(index);
		query.setMaxResults(count);
		
		pageBean.setPageData(query.list());
	}
	
	public int getTotalCount(String listType, int type) {
		String hql = "select count(*) from Goods where listType = ? and type = ? and state=1";
		Object[] params = {listType,type};
		int totalCount = Integer.parseInt(String.valueOf(getHibernateTemplate()
				.find(hql, params).iterator().next()));		
		return totalCount;
	}
	
	public void getGoodsByPriceUp(String listType,PageBean<Goods> pageBean) {
	       int totalCount = getTotalCount(listType);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			  
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where  listType = ? and state=1 order by price asc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setFirstResult(index);
			query.setMaxResults(count);
			
			pageBean.setPageData(query.list());
		}
	
	public void getGoodsByPriceUp(String listType,int type,PageBean<Goods> pageBean) {
		
       int totalCount = getTotalCount(listType,type);
		
		pageBean.setTotalCount(totalCount);
		
		if (pageBean.getCurrentPage() <=0) {
			pageBean.setCurrentPage(1);					    
		} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
			pageBean.setCurrentPage(pageBean.getTotalPage());		
		}
		
		int currentPage = pageBean.getCurrentPage();
		int index = (currentPage-1)*pageBean.getPageSize();
		int count = pageBean.getPageSize();
		
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		org.hibernate.classic.Session session = factory.openSession();
		
		String hql = "from Goods where  listType = ? and  type=?  and state=1 order by price asc";
		Query query = session.createQuery(hql);
		query.setParameter(0, listType);
		query.setParameter(1, type);
		query.setFirstResult(index);
		query.setMaxResults(count);
		
		pageBean.setPageData(query.list());
	}

	public void getGoodsByPriceDwon(String listType,PageBean<Goods> pageBean) {

		 int totalCount = getTotalCount(listType);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and state=1 order by price desc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setFirstResult(index);
			query.setMaxResults(count);
			
			pageBean.setPageData(query.list());
	}
	
	public void getGoodsByPriceDwon(String listType,int type,PageBean<Goods> pageBean) {

		 int totalCount = getTotalCount(listType,type);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and  type = ? and state=1 order by price desc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setParameter(1, type);
			query.setFirstResult(index);
			query.setMaxResults(count);
			
			pageBean.setPageData(query.list());
	}
	
	public void getRecentlyGoods(String listType,PageBean<Goods> pageBean) {

		 int totalCount = getTotalCount(listType);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and state=1 order by createDate desc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setFirstResult(index);
			query.setMaxResults(count);
			
			pageBean.setPageData(query.list());
	}

	public void getRecentlyGoods(String listType,int type,PageBean<Goods> pageBean) {

		 int totalCount = getTotalCount(listType,type);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and type =? and state=1 order by createDate desc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setParameter(1, type);
			query.setFirstResult(index);
			query.setMaxResults(count);
			
			pageBean.setPageData(query.list());
	}

	public void getGoodsByListTypePage(String listType, String name,
			PageBean<Goods> pageBean) {
	    int totalCount = getTotalCount(listType,name);
		
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and  goodName like:name and state=1";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setString("name", "%"+name+"%");
			query.setFirstResult(index);
			query.setMaxResults(count);		
			pageBean.setPageData(query.list());		
	}
	
	public int getTotalCount(String listType,String name) {
		String hql = "select count(*) from Goods where listType = ? and goodName like ? and state=1";
		Object[] params = {listType,"%"+name+"%"};
		int totalCount = Integer.parseInt(String.valueOf(getHibernateTemplate()
				.find(hql, params).iterator().next()));		
		return totalCount;
	}

	public void getGoodsByListTypePage(String listType, int type, String name,
			PageBean<Goods> pageBean) {
        int totalCount = getTotalCount(listType,type,name);
		
		pageBean.setTotalCount(totalCount);
		
		if (pageBean.getCurrentPage() <=0) {
			pageBean.setCurrentPage(1);					    
		} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
			pageBean.setCurrentPage(pageBean.getTotalPage());		
		}
		
		int currentPage = pageBean.getCurrentPage();
		int index = (currentPage-1)*pageBean.getPageSize();
		int count = pageBean.getPageSize();
		
		
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		org.hibernate.classic.Session session = factory.openSession();
		
		String hql = "from Goods where listType = ? and type =? and goodName like:name and state=1";
		Query query = session.createQuery(hql);
		query.setParameter(0, listType);
		query.setParameter(1, type);
		query.setParameter("name", "%"+name+"%");
		query.setFirstResult(index);
		query.setMaxResults(count);		
		pageBean.setPageData(query.list());	
	}
	public int getTotalCount(String listType, int type,String name) {
		String hql = "select count(*) from Goods where listType = ? and type = ? and goodName like ? and state=1";
		Object[] params = {listType,type,"%"+name+"%"};
		int totalCount = Integer.parseInt(String.valueOf(getHibernateTemplate()
				.find(hql, params).iterator().next()));		
		return totalCount;
	}

	public void getGoodsByPriceUp(String listType, int type, String name,
			PageBean<Goods> pageBean) {
		  int totalCount = getTotalCount(listType,type,name);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and type =? and goodName like:name and state=1 order by price asc ";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setParameter(1, type);
			query.setParameter("name", "%"+name+"%");
			query.setFirstResult(index);
			query.setMaxResults(count);		
			pageBean.setPageData(query.list());	
		
	}

	public void getGoodsByPriceDwon(String listType, int type, String name,
			PageBean<Goods> pageBean) {
		  int totalCount = getTotalCount(listType,type,name);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and type =? and goodName like:name and state=1 order by price desc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setParameter(1, type);
			query.setParameter("name", "%"+name+"%");
			query.setFirstResult(index);
			query.setMaxResults(count);		
			pageBean.setPageData(query.list());	
		
	}

	public void getRecentlyGoods(String listType, int type, String name,
			PageBean<Goods> pageBean) {
		  int totalCount = getTotalCount(listType,type,name);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and type =? and goodName like:name and state=1 order by createDate desc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setParameter(1, type);
			query.setParameter("name", "%"+name+"%");
			query.setFirstResult(index);
			query.setMaxResults(count);		
			pageBean.setPageData(query.list());	
		
	}

	public void getGoodsByPriceUp(String listType, String name,
			PageBean<Goods> pageBean) {
		    int totalCount = getTotalCount(listType,name);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where  listType = ? and  goodName like:name and state=1 order by price asc";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setParameter("name", "%"+name+"%");
			query.setFirstResult(index);
			query.setMaxResults(count);		
			pageBean.setPageData(query.list());	
		
	}

	public void getGoodsByPriceDwon(String listType, String name,
			PageBean<Goods> pageBean) {
		
		 int totalCount = getTotalCount(listType,name);
		
		pageBean.setTotalCount(totalCount);
		
		if (pageBean.getCurrentPage() <=0) {
			pageBean.setCurrentPage(1);					    
		} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
			pageBean.setCurrentPage(pageBean.getTotalPage());		
		}
		
		int currentPage = pageBean.getCurrentPage();
		int index = (currentPage-1)*pageBean.getPageSize();
		int count = pageBean.getPageSize();
		
		
		Configuration cfg = new Configuration().configure();
		SessionFactory factory = cfg.buildSessionFactory();
		org.hibernate.classic.Session session = factory.openSession();
		
		String hql = "from Goods where listType = ? and goodName like:name and state=1 order by price desc";
		Query query = session.createQuery(hql);
		query.setParameter(0, listType);
		query.setParameter("name", "%"+name+"%");
		query.setFirstResult(index);
		query.setMaxResults(count);		
		pageBean.setPageData(query.list());	
		
	}

	public void getRecentlyGoods(String listType, String name,
			PageBean<Goods> pageBean) {
		  int totalCount = getTotalCount(listType,name);
			
			pageBean.setTotalCount(totalCount);
			
			if (pageBean.getCurrentPage() <=0) {
				pageBean.setCurrentPage(1);					    
			} else if (pageBean.getCurrentPage() > pageBean.getTotalPage()){
				pageBean.setCurrentPage(pageBean.getTotalPage());		
			}
			
			int currentPage = pageBean.getCurrentPage();
			int index = (currentPage-1)*pageBean.getPageSize();
			int count = pageBean.getPageSize();
			
			
			Configuration cfg = new Configuration().configure();
			SessionFactory factory = cfg.buildSessionFactory();
			org.hibernate.classic.Session session = factory.openSession();
			
			String hql = "from Goods where listType = ? and goodName like:name and state=1";
			Query query = session.createQuery(hql);
			query.setParameter(0, listType);
			query.setParameter("name", "%"+name+"%");
			query.setFirstResult(index);
			query.setMaxResults(count);		
			pageBean.setPageData(query.list());	
		
	}
	
	public List<Goods> getGoodByUserId(String userId) {
		String hql = "from Goods where userId = ?";
		List<Goods> goods = getHibernateTemplate().find(hql, userId);
		return goods;
	}

	public List<Goods> getGoodByUserId(String userId, String listType) {
		String hql = "from Goods where userId = ? and listType = ?";
		Object[] param = {userId,listType};
		List<Goods> goods = getHibernateTemplate().find(hql, param);
		return goods;
	}

	public List<Goods> getGoodByUserId(String userId, String listType, int state) {
		String hql = "from Goods where userId = ? and listType = ? and state = ?";
		Object[] param = {userId,listType,state};
		List<Goods> goods = getHibernateTemplate().find(hql, param);
		return goods;
	}

	public Goods getGoodBygoodId(String goodId) {
		return (Goods) getHibernateTemplate().get(Goods.class, goodId);
	}

	public List<Goods> getHotGoods() {
	    String hql = "from Goods where state=1 and listType='sale' order by createDate desc";
		return getHibernateTemplate().find(hql).subList(0, 8);
	}
}
